//
//  NSString+Calculate.m
//  Test
//
//  Created by xiaokang on 2017/3/28.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "NSString+Calculate.h"
#import "NSString+Predicate.h"

@implementation NSString (Calculate)

+ (NSString *)calculateNumberOne:(NSString *)numberOne calculateType:(CalculateType)type numberTwo:(NSString *)numberTwo {
    
    if (![numberOne isPureFloat]) {
        numberOne = @"0";
    }
    if (![numberTwo isPureFloat]) {
        numberTwo = @"0";
    }
    
    NSDecimalNumber *decimalnumberOne = [NSDecimalNumber decimalNumberWithString:numberOne];
    NSDecimalNumber *decimalnumberTwo = [NSDecimalNumber decimalNumberWithString:numberTwo];
    
    
    switch (type) {
        case 0:return [[decimalnumberOne decimalNumberByAdding:decimalnumberTwo] stringValue];
        case 1:return [[decimalnumberOne decimalNumberBySubtracting:decimalnumberTwo] stringValue];
        case 2:return [[decimalnumberOne decimalNumberByMultiplyingBy:decimalnumberTwo] stringValue];
        case 3:return [[decimalnumberOne decimalNumberByDividingBy:decimalnumberTwo] stringValue];
    }
}



+ (NSString *)calculateNumberOne:(NSString *)numberOne calculateType:(CalculateType)type numberTwo:(NSString *)numberTwo withMode:(NSRoundingMode)roundingMode scale:(short)scale{
    
    if (![numberOne isPureFloat]) {
        numberOne = @"0";
    }
    if (![numberTwo isPureFloat]) {
        numberTwo = @"0";
    }
    
    NSDecimalNumber *decimalnumberOne = [NSDecimalNumber decimalNumberWithString:numberOne];
    NSDecimalNumber *decimalnumberTwo = [NSDecimalNumber decimalNumberWithString:numberTwo];
    
    
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMode
                                                                                                      scale:scale
                                                                                           raiseOnExactness:NO
                                                                                            raiseOnOverflow:NO
                                                                                           raiseOnUnderflow:NO
                                                                                        raiseOnDivideByZero:NO];
    
    switch (type) {
        case 0:return [[decimalnumberOne decimalNumberByAdding:decimalnumberTwo withBehavior:roundingBehavior] stringValue];
        case 1:return [[decimalnumberOne decimalNumberBySubtracting:decimalnumberTwo withBehavior:roundingBehavior] stringValue];
        case 2:return [[decimalnumberOne decimalNumberByMultiplyingBy:decimalnumberTwo withBehavior:roundingBehavior] stringValue];
        case 3:return [[decimalnumberOne decimalNumberByDividingBy:decimalnumberTwo withBehavior:roundingBehavior] stringValue];
    }
    
}

@end
